import Joi from "joi";
import { ObjectId } from "mongodb";
import { Context } from "koa";

import validate from "../libs/validate";
import { JsonResp } from "../libs/stats";
import * as projectService from "../services/project.service";

// 项目列表
export async function list(ctx: Context) {
  const account = ctx.state.user.account;
  const result = await projectService.list(account);
  ctx.body = new JsonResp(result);
}

// 添加项目
export async function create(ctx: Context) {
  const data = validate(
    ctx.request.body,
    Joi.object({
      name: Joi.string()
        .min(2)
        .max(10)
        .required()
        .error(new Error("项目名称为2到10个字符!")),
      organization: Joi.string()
        .min(2)
        .max(10)
        .required()
        .error(new Error("部门名称为2到10个字符!")),
    })
  );

  data.personId = ctx.state.user._id;
  data.starSign = false;
  data.createdAt = new Date();
  await projectService.create(data);
  ctx.body = new JsonResp();
}

// 删除项目
export async function remove(ctx: Context) {}

// 搜索项目
export async function search(ctx: Context) {}
